import config from '@/config/config.js';
import axios from 'axios';

const itemRequest = {
    loadItemList (searchItem, callback) {
        const url = `${config.mdBaseUrl}/item/query`;
        axios.get(url, {
            params: {
                itemName: searchItem.itemName || null,
                itemCode: searchItem.itemCode || null,
                itemTypeId: searchItem.itemTypeId || null,
                unitId: searchItem.unitId || null,
                itemStatus: searchItem.itemStatus || null,
            },
            headers: {
                token: localStorage.getItem("token") // 传递token进行身份验证
            }
        }).then(res => {
            if (res.data && res.data.code === 200) {  // 假设返回结果中有 code 字段来标识成功状态
                callback(res.data);
            } else {
                console.error('请求失败:', res.data.message);
            }
        })
    },
    addItem(item, callback){
        const url = `${config.mdBaseUrl}/item/add`;
        axios.post(url, item, {
            headers: {
                token: localStorage.getItem("token")
            }
        }).then(res => {
            callback(res.data);
        }).catch(err => {
            console.error("请求失败:", err);
        });
    },
    loadUpdateInfo(itemId, callback){
        const url = `${config.mdBaseUrl}/item/queryItemById?itemId=${itemId}`;
        axios.get(url, {
            headers: {
                token: localStorage.getItem("token")
            }
        }).then(res => {
            callback(res.data);
        }).catch(err => {
            console.error("请求失败:", err);
        });
    },
    EditDateSubmit(item, callback){
        const url = `${config.mdBaseUrl}/item/update`;
        axios.post(url, item, {
            headers: {
                token: localStorage.getItem("token")
            }
        }).then(res => {
            callback(res.data);
        }).catch(err => {
            console.error("请求失败:", err);
        });
    },
    deleteItems(items, callback){
        const url = `${config.mdBaseUrl}/item/deletes`;
        axios.post(url, items, {
            headers: {
                token: localStorage.getItem("token")
            }
        }).then(res => {
            callback(res.data);
        }).catch(err => {
            console.error("请求失败:", err);
        });
    },
    loadItemBomList(itemId, callback){
        const url = `${config.mdBaseUrl}/item/queryItemBomList?itemId=${itemId}`;
        axios.get(url, {
            headers: {
                token: localStorage.getItem("token")
            }
        }).then(res => {
            callback(res.data);
        }).catch(err => {
            console.error("请求失败:", err);
        });
    },
    addItemToBom(items, callback){
        axios.post(`${config.mdBaseUrl}/item/addItemToBom`, items, {
            headers: {
                token: localStorage.getItem("token")
            }
        }).then(res => {
            callback(res.data);
        }).catch(err => {
            console.error("请求失败:", err);
        });
    },
    selectItemBomById(bomId, callback){
        axios.get(`${config.mdBaseUrl}/item/selectItemBomById?bomId=${bomId}`, {
            headers: {
                token: localStorage.getItem("token")
            }
        }).then(res => {
            callback(res.data);
        }).catch(err => {
            console.error("请求失败:", err);
        });
    },
    itemBomEditDateSubmit(itemBom, callback){
        axios.post(`${config.mdBaseUrl}/item/itemBomEditDateSubmit`, itemBom, {
            headers: {
                token: localStorage.getItem("token")
            }
        }).then(res => {
            callback(res.data);
        }).catch(err => {
            console.error("请求失败:", err);
        });
    },
    querySipByItemId(itemId, callback){
        axios.get(`${config.mdBaseUrl}/item/querySipByItemId?itemId=${itemId}`, {
            headers: {
                token: localStorage.getItem("token")
            }
        }).then(res => {
            callback(res.data);
        }).catch(err => {
            console.error("请求失败:", err);
        });
    },
    querySipById(sipId, callback){
        axios.get(`${config.mdBaseUrl}/item/querySipById?sipId=${sipId}`, {
            headers: {
                token: localStorage.getItem("token")
            }
        }).then(res => {
            callback(res.data);
        }).catch(err => {
            console.error("请求失败:", err);
        });
    },
    queryProcessList(processId, callback){
        axios.get(`${config.mdBaseUrl}/item/queryProcessList?processId=${processId}`, {
            headers: {
                token: localStorage.getItem("token")
            }
        }).then(res => {
            callback(res.data);
        }).catch(err => {
            console.error("请求失败:", err);
        });
    },
    addSip(sip, callback){
        axios.post(`${config.mdBaseUrl}/item/addSip`, sip, {
            headers: {
                token: localStorage.getItem("token")
            }
        }).then(res => {
            callback(res.data);
        }).catch(err => {
            console.error("请求失败:", err);
        });
    },
    querySopByItemId(itemId, callback){
        axios.get(`${config.mdBaseUrl}/item/querySopByItemId?itemId=${itemId}`, {
            headers: {
                token: localStorage.getItem("token")
            }
        }).then(res => {
            callback(res.data);
        }).catch(err => {
            console.error("请求失败:", err);
        });
    },
    querySopById(sopId, callback){
        axios.get(`${config.mdBaseUrl}/item/querySopById?sopId=${sopId}`, {
            headers: {
                token: localStorage.getItem("token")
            }
        }).then(res => {
            callback(res.data);
        }).catch(err => {
            console.error("请求失败:", err);
        });
    },
    addSop(sop, callback){
        axios.post(`${config.mdBaseUrl}/item/addSop`, sop, {
            headers: {
                token: localStorage.getItem("token")
            }
        }).then(res => {
            callback(res.data);
        }).catch(err => {
            console.error("请求失败:", err);
        });
    }
};

export default itemRequest;